import { defineStore } from 'pinia';
import { SystemRoleMenuInfoVO } from '@/entiy/vo/SystemRoleMenuInfoVO.ts';
import RequestService from '@/service/RequestService.ts';
import PromiseUtils from '@/utils/PromiseUtils.ts';
import { SystemRoleInfoVO } from '@/entiy/vo/SystemRoleInfoVO.ts';
import { MenuUtils } from '@/utils/MenuUtils.ts';
import { routeConfigs } from '@/router/config.ts';
import { LocalMenuInfoVO } from '@/entiy/vo/LocalMenuInfoVO.ts';
import { DialogUtils } from "@/utils/DialogUtils.ts";

export const useSystemRoleMenuComponentStore = defineStore('systemRoleMenuComponentStore', {
    state: () => {
        return {
            isOpen: false,
            isLoading: false,
            tableDataList: [] as Array<SystemRoleMenuInfoVO>,
        };
    },

    getters: {},

    actions: {
        // 加载表格数据
        async loadTableData(systemRoleInfo: SystemRoleInfoVO) {
            const roleCode = systemRoleInfo.roleCode;
            const params = {roleCode: roleCode};

            // 查询角色已经配置的菜单信息
            const result = await RequestService.post<Array<SystemRoleMenuInfoVO>>('query-role-menu-info-list', params);
            if (result.isError) {
                return PromiseUtils.reject(result.errorMsg);
            }

            // 角色配置的菜单信息
            const roleMenuInfoList = result.bizDta;
            const roleMenuInfoSet = new Set(roleMenuInfoList.map((item: SystemRoleMenuInfoVO) => item.routeName));
            console.log('RoleMenuInfoSet: ', roleMenuInfoSet);

            // 将本地路由转换成本地菜单信息
            const localMenuInfoList: LocalMenuInfoVO[] = MenuUtils.flattenRouteToMenu(routeConfigs);
            console.log('LocalMenuInfoList: ', localMenuInfoList);

            // 合并本地菜单信息和角色已配置的菜单信息
            const finalRoleMenuInfoList: SystemRoleMenuInfoVO[] = [];
            for (let localMenuInfoVO of localMenuInfoList) {
                const systemRoleMenuInfoVO = {
                    id: 0,
                    roleCode: roleCode,
                    menuTitle: localMenuInfoVO.menuTitle,
                    routeName: localMenuInfoVO.routeName,
                    isSelected: true
                } as SystemRoleMenuInfoVO;

                systemRoleMenuInfoVO.isSelected = roleMenuInfoSet.has(localMenuInfoVO.routeName);

                finalRoleMenuInfoList.push(systemRoleMenuInfoVO);
            }

            this.tableDataList = finalRoleMenuInfoList;
            return PromiseUtils.resolve();
        },

        // 保存角色菜单信息
        async saveRoleMenuInfoList() {
            const dataList = this.tableDataList.filter((item: SystemRoleMenuInfoVO) => item.isSelected)
            const params = {dataList: dataList};

            // 保存角色菜单信息
            const result = await RequestService.post<Array<SystemRoleMenuInfoVO>>('/save-role-menu-info', params);
            if (result.isError) {
                return PromiseUtils.reject(result.errorMsg);
            }

            return PromiseUtils.resolve();
        },

        // 打开并重新加载数据
        async openAndReloadTableData(systemRoleInfo: SystemRoleInfoVO) {
            this.isOpen = true;
            this.isLoading = true;
            this.loadTableData(systemRoleInfo).then(() => {
                this.isLoading = false;
            }).catch(() => {
                this.isOpen = false;
                this.isLoading = false;
                DialogUtils.showUniqueDialog('加载数据失败, 请稍后再试!')
            });
        }
    }
});